package cn.iocoder.yudao.module.nginxui.controller.admin.httpserver;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nginxui.controller.admin.httpserver.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httpserver.HttpServerDO;
import cn.iocoder.yudao.module.nginxui.service.httpserver.HttpServerService;

@Tag(name = "管理后台 - Nginx HTTP下的Server块配置")
@RestController
@RequestMapping("/nginxui/http-server")
@Validated
public class HttpServerController {

    @Resource
    private HttpServerService httpServerService;

    @PostMapping("/create")
    @Operation(summary = "创建Nginx HTTP下的Server块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:http-server:create')")
    public CommonResult<Long> createHttpServer(@Valid @RequestBody HttpServerSaveReqVO createReqVO) {
        return success(httpServerService.createHttpServer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Nginx HTTP下的Server块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:http-server:update')")
    public CommonResult<Boolean> updateHttpServer(@Valid @RequestBody HttpServerSaveReqVO updateReqVO) {
        httpServerService.updateHttpServer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Nginx HTTP下的Server块配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nginxui:http-server:delete')")
    public CommonResult<Boolean> deleteHttpServer(@RequestParam("id") Long id) {
        httpServerService.deleteHttpServer(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除Nginx HTTP下的Server块配置")
                @PreAuthorize("@ss.hasPermission('nginxui:http-server:delete')")
    public CommonResult<Boolean> deleteHttpServerList(@RequestParam("ids") List<Long> ids) {
        httpServerService.deleteHttpServerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Nginx HTTP下的Server块配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nginxui:http-server:query')")
    public CommonResult<HttpServerRespVO> getHttpServer(@RequestParam("id") Long id) {
        HttpServerDO httpServer = httpServerService.getHttpServer(id);
        return success(BeanUtils.toBean(httpServer, HttpServerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Nginx HTTP下的Server块配置分页")
    @PreAuthorize("@ss.hasPermission('nginxui:http-server:query')")
    public CommonResult<PageResult<HttpServerRespVO>> getHttpServerPage(@Valid HttpServerPageReqVO pageReqVO) {
        PageResult<HttpServerDO> pageResult = httpServerService.getHttpServerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HttpServerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Nginx HTTP下的Server块配置 Excel")
    @PreAuthorize("@ss.hasPermission('nginxui:http-server:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHttpServerExcel(@Valid HttpServerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HttpServerDO> list = httpServerService.getHttpServerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Nginx HTTP下的Server块配置.xls", "数据", HttpServerRespVO.class,
                        BeanUtils.toBean(list, HttpServerRespVO.class));
    }

}